package net.kevin.demo.hessian.client;

import java.net.MalformedURLException;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.caucho.hessian.client.HessianProxyFactory;

import net.kevin.demo.hessian.service.BasicAPI;

public class HissianClient {
	private static HessianProxyFactory factory = new HessianProxyFactory();
	private static String hessianServlet_url = "http://localhost:8081/cms/hello";
	private static String springHessian_url = "http://localhost:8081/cms/remote/helloSpring";

	private static String rpc_url = "http://localhost:8081/cms/remote/rpcServer";

	public static void main(String[] args) throws MalformedURLException {
		hessianServlet();
		springHessian();
		rpcHessian();
		springClient();
		springAOPClient();
		//refTest();
	}

	public static void hessianServlet() throws MalformedURLException {
		BasicAPI basic = (BasicAPI) factory.create(BasicAPI.class, hessianServlet_url);
		System.out.println("hessianServlet >> hello(): " + basic.sayHello());
	}

	public static void springHessian() throws MalformedURLException {
		BasicAPI basic = (BasicAPI) factory.create(BasicAPI.class, springHessian_url);
		System.out.println("springHessian >> hello(): " + basic.sayHello());
	}

	public static void rpcHessian() throws MalformedURLException {
		String url = rpc_url + "/" + "cms" + "?interface=" + BasicAPI.class.getName();
		BasicAPI basic = (BasicAPI) factory.create(BasicAPI.class, url);
		System.out.println("rpcHessian >> hello(): " + basic.sayHello());
	}

	public static void springClient() {
		@SuppressWarnings("resource")
		ApplicationContext act = new ClassPathXmlApplicationContext("spring-context.xml");
		BasicAPI basic = (BasicAPI) act.getBean("basicAPIImpl");
		System.out.println("springClient >> hello(): " + basic.sayHello());
	}

	public static void springAOPClient() {
		ApplicationContext act = new ClassPathXmlApplicationContext("spring-context.xml");
		BasicAPI basic = (BasicAPI) act.getBean("basicAPIRemote");
		System.out.println("springAOPClient >> hello(): " + basic.sayHello());
	}
	
	public static void refTest() {
		ApplicationContext act = new ClassPathXmlApplicationContext("spring-context.xml");
		BasicAPI basic = (BasicAPI) act.getBean("basicAPIRemote");
		BasicAPI basic2 = basic.getBasicAPI2();//复杂的对象不行
		System.out.println("springAOPClient >> hello(): " + basic2.sayHello());
	}
}
